<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends BaseController {
    //默认处理
    public function index(){
		if(session('?user')){
			$user=session('user');
			$name=$user['hosp'];
			$this->assign('name',$name);
			$this->display('index');
		}
        else{		
            $this->display('login');
		}
    }
    //处理登录
    public function login(){
        $UserInfo = M('Userinfo');
        //$Hospital = M('Hospital');
        $condition['loginname']=I('loginName');
        $condition['passwd']= md5(I('password'));
        //$condition['hid']=I('hid');
        $result = $UserInfo->db(2,'DB_CONFIG2')->where($condition)->find();
        if($result){
            $user = array();
            $user['userid'] = $result['userid'];
            $user['loginname'] = $result['loginname'];
            //$user['isadmin'] = $result['isadmin'];
            //$user['employname'] = $result['employname'];
            //$hid = $result['hid'];
            //测试权限
			//$isadmin=$result['isadmin'];
			//if($isadmin==0){//非管理员
            //    $user['priv'] = $result['actionpriv'];
			//}
			//else{
			//	$user['priv']=999;
			//}
            //$user['hid'] = $hid;
            //$res = $Hospital->where("hid = $hid")->find();
            //$user['hosp'] =$res['name'];
            $_SESSION['user'] = $user;
			$access_token=md5($user['loginname']);//验证信息
			$access_token=array('access_token'=>$access_token);
            $this->successreturn($access_token);
        }else{
			$access_token=array('access_token'=>0);
            $this->errorreturn($access_token);
        }
    }
    
    //退出登录
    public function logout(){
        session('[destroy]');
		$access_token=array('access_token'=>0);
        $this->successreturn('0',$access_token);
    }


    
    //处理密码修改
    public function changePass(){
        $oldpass = I('oldPass');
        $newpass = I('newPass');
        $UserInfo = M('Userinfo');
        $userid = $_SESSION['user']['userid'];
        $result = $UserInfo->db(1,'DB_CONFIG1')->where("userid=$userid")->find();
        if($result){
            if($result['passwd']==md5($oldpass)){
                $data['userid'] = $userid;
                $data['passwd'] = md5($newpass);
                $UserInfo->save($data);
                $this->successreturn('修改成功！');
            }else{
                $this->errorreturn('旧密码有误，请重新输入!');
            }
        }else{
            $this->errorreturn('用户未找到!');
        }
    }    
    
    //导航列表
    /*public function getNav(){
        $user = $_SESSION['user'];
        if(!isset($_SESSION['user'])) return;
		//if($user['priv']['cust']||$user['priv']['zhgl']){
		    $btn['id'] = 'hcustomer';
            $btn['name'] = '首页数据';
            $data[] = $btn;
		//} 
        if($user['priv']['cust']||$user['priv']['zhgl']){
            $btn['id'] = 'other';
            $btn['name'] = '基本信息';
            $data[] = $btn;        
        }                  
        
        if($user['priv']['coll']){
            $btn['id'] = 'collect';
            $btn['name'] = '处置管理';
            $data[] = $btn;
        }
        if($user['priv']['repo']){
            $btn['id'] = 'report';
            $btn['name'] = '统计报表';
            $data[] = $btn;
        }
        if($user['priv']['logview']){
            $btn['id'] = 'logview';
            $btn['name'] = '日志查看';
            $data[] = $btn;
        }
        $btn['id'] = 'changepass';
        $btn['name'] = '密码修改';
        $data[] = $btn;
        if($user['priv']['syst']){
            $btn['id'] = 'setup';
            $btn['name'] = '系统设置';
            $data[] = $btn;
        }
        $btn['id'] = 'logout';
        $btn['name'] = '登出系统';
        $data[] = $btn;
        $this->ajaxReturn($data);
    }*/
	public function priv(){//权限传递
		$priv=session('user.priv');
		$this->ajax($priv);
	}
}